<?php
/**
 * Saves files and returns filename
 * @author Gustav Knutsson
 */
class Filewriter{
	
	// Filename of the saved file.
	public $filename;
	
	/**
	 * Writes file to the storage then returns the new filename
	 * 
	 * @param INT $fileid
	 * @param STRING Base64 encoded data $filedata
	 * @return STRING
	 * 
	 * @author Gustav Knutsson
	 */
	public function __construct($fileid, $filedata){
		$st = JABBERDRIVE_STORAGE_AREA;
		if($st{strlen($st) - 1} != "/") $st = $st."/";
		
		$file = JABBERDRIVE_STORAGE_AREA.$fileid;
		if(file_exists($file)){
			die("<b>jdError:</b> File exists!");
		}else{
			$fh = fopen($file,"w+") or die("<b>jdError:</b> Could not open file for writing");
			$content = urldecode(base64_decode($filedata));
			fwrite($fh, $content, strlen($content)) or die("<b>jdError:</b> Could not write to file");
			@fclose($fh);
			
			$this->filename = $fileid;
		}
	}
}
?>